{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "### 写个脚本统计下sp3 ISO 的信息\n",
    "- 抓取所有二进制包对应的源码包\n",
    "- 包括他的summer信息，自动化翻译下\n",
    "- 形成表格\n",
    "- 同时按底层软件，应用软件，数据库等类型分类\n",
    "- 把操作系统软件包组成，弄清楚了"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'.'\n",
      "b'..'\n",
      "b'09723533f3b925d8837d6ad5541b.x'\n",
      "b'0b8c173b7e683e3c95693c5d8b4.z'\n",
      "b'13d2099702b7f6898e792b13350.zc'\n",
      "b'30e6c282f4a80904b6761dec9f8f.x'\n",
      "b'69d39807887f59167a667d2a5374.g'\n",
      "b'756e7a5c77ca2c94de54fd562440.g'\n",
      "b'86a13bf345e75764abdaf4f0ddf8.'\n",
      "b'99c9e112562b7c986cede71d0620.g'\n",
      "b'9d078581db64877d115e36ee67b2.x'\n",
      "b'TRANS.TBL'\n",
      "b'c89203a1ee9eca474ad727a3946.zc'\n",
      "b'e568c0f1937ef22de2cd8837804.xm'\n",
      "b'e98485648bcff41787f1b45c670.zc'\n",
      "b'repomd.xml'\n"
     ]
    }
   ],
   "source": [
    "import pycdlib\n",
    "\n",
    "iso = pycdlib.PyCdlib()\n",
    "iso.open(\"D:/ISO/Fedora-Server-dvd-x86_64-36-1.5.iso\")\n",
    "for child in iso.list_children(iso_path='/repodata'):\n",
    "    print(child.file_identifier())\n",
    "iso.get_file_from_iso(\"iso_test/primary.sqlite.xz\", iso_path=\"/repodata/09723533f3b925d8837d6ad5541b.x\")\n",
    "iso.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [],
   "source": [
    "import lzma\n",
    "import shutil\n",
    "\n",
    "input_xzfile = 'iso_test/primary.sqlite.xz'\n",
    "output_file = 'iso_test/primary.sqlite'\n",
    "with lzma.open(input_xzfile, 'rb') as input:\n",
    "    with open(output_file, 'wb') as output:\n",
    "        shutil.copyfileobj(input, output)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 从数据库里抽信息"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [],
   "source": [
    "import sqlite3\n",
    "\n",
    "\n",
    "def get_db():\n",
    "    return sqlite3.connect(\"iso_test/primary.sqlite\")\n",
    "\n",
    "\n",
    "def deal_select(sql, db_conn=None):\n",
    "    if db_conn is None:\n",
    "        db_conn = get_db()\n",
    "    cursor = db_conn.cursor()\n",
    "    cursor.execute(sql)\n",
    "    rs = cursor.fetchall()\n",
    "    return rs"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "所有的组： ['Unspecified', 'System Environment/Base', 'Development/Libraries', 'Documentation', 'System Environment/Kernel']\n"
     ]
    }
   ],
   "source": [
    "# 获取所有组\n",
    "sql_str = \"select distinct rpm_group from packages\"\n",
    "group_all = [g_o[0] for g_o in deal_select(sql_str)]\n",
    "print(\"所有的组：\", group_all)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 翻译"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好，世界！\n",
      "这是第2段。\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "import requests\n",
    "from hashlib import md5\n",
    "\n",
    "\n",
    "def make_md5(s, encoding='utf-8'):\n",
    "    return md5(s.encode(encoding)).hexdigest()\n",
    "\n",
    "\n",
    "def translate(en_str='Hello World!\\nThis is 2nd paragraph.', appid='20210111000668489', appkey='XP9oeg7cfrADX3Rf9dGc'):\n",
    "    url = 'http://api.fanyi.baidu.com/api/trans/vip/translate'\n",
    "    salt = random.randint(32768, 65536)\n",
    "    sign = make_md5(appid + en_str + str(salt) + appkey)\n",
    "    headers = {'Content-Type': 'application/x-www-form-urlencoded'}\n",
    "    payload = {'appid': appid, 'q': en_str, 'from': 'en', 'to': 'zh', 'salt': salt, 'sign': sign}\n",
    "    r = requests.post(url, params=payload, headers=headers)\n",
    "    return r.json()\n",
    "\n",
    "\n",
    "def translate_res_join(res_json):\n",
    "    return \"\\n\".join([tran_one[\"dst\"] for tran_one in res_json.get(\"trans_result\", [])])\n",
    "\n",
    "\n",
    "print(translate_res_join(translate()))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}